/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.dynread.com
 * 注意：
 * 本软件为www.dynread.com开发研制，项目使用请保留此说明
 */
package com.dynread.mall.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dynread.common.utils.SecurityUtils;
import com.dynread.mall.entity.GoodsSpu;
import com.dynread.mall.mapper.GoodsSpuMapper;
import com.dynread.mall.service.GoodsSpuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * spu商品
 *
 * @author drinvay
 * @date 2022/5/6 13:16
 */
@Service
public class GoodsSpuServiceImpl extends ServiceImpl<GoodsSpuMapper, GoodsSpu> implements GoodsSpuService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        super.removeById(id);
        return true;
    }

    @Override
    public IPage<GoodsSpu> page1(IPage<GoodsSpu> page, GoodsSpu goodsSpu) {
        return baseMapper.selectPage1(page, goodsSpu);
    }

    /**
     * 新增商品
     *
     * @param entity
     * @return boolean
     * @author drinvay
     * @date 2022/5/6 11:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(GoodsSpu entity) {
        entity.setCreateBy(SecurityUtils.getUsername());
        return save(entity);
    }

    /**
     * 修改商品
     *
     * @param entity
     * @return boolean
     * @author drinvay
     * @date 2022/5/6 11:05
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modify(GoodsSpu entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        return updateById(entity);
    }

    /**
     * 更新上下架状态
     *
     * @param shelf
     * @param ids
     * @return boolean
     * @author drinvay
     * @date 2022/5/6 12:11
     */
    @Override
    public boolean updateShelfStatus(String shelf, String ids) {
        GoodsSpu goodsSpu = new GoodsSpu();
        goodsSpu.setShelf(shelf);
		goodsSpu.setUpdateBy(SecurityUtils.getUsername());
        return update(goodsSpu, Wrappers.<GoodsSpu>lambdaQuery().in(GoodsSpu::getId, Convert.toList(ids)));
    }

    @Override
    public GoodsSpu getById1(String id) {
        return baseMapper.selectById1(id);
    }

    @Override
    public GoodsSpu getById2(String id) {
        return baseMapper.selectById2(id);
    }

}
